﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace MyGame
{
    public enum UIType
    {   
        EnterGameUI,        
        BackGroundUI,
        SelectModelUI,
        BoardUI,
    }

    public class UIManager : BaseManager<UIManager>
    {
        public Dictionary<UIType, GameObject> uiDictionary = new Dictionary<UIType, GameObject>();
        private readonly string[] uiLists = Enum.GetNames(typeof(UIType));

        private void Awake()
        {
            DontDestroyOnLoad(this);
        }

        private void Start()
        {
           
        }

        public string[] GetUIList()
        {
            return uiLists;
        }

        public void Init()
        {
            ShowUI(UIType.BoardUI);
            BoardUI.Instance.Init();
            BackGroundUI.Instance.UpdateTextInit(_black_Name: "黑方:玩家1", _white_Name: "白方:电脑", _black_Index: "黑方步数:0", _white_Index: "白方步数:0", executor_Play:"当前执方:黑棋");
        }

        public void EntetGame()
        {
            HideUI(UIType.EnterGameUI);
            ShowUI(UIType.SelectModelUI);
        }

        public void EnterSelect()
        {
            HideUI(UIType.SelectModelUI);
            ShowUI(UIType.BackGroundUI);
            BackGroundUI.Instance.Init();
        }

        public void GameOver()
        {
            HideUI(UIType.BoardUI);
            HideUI(UIType.BackGroundUI);
            ShowUI(UIType.SelectModelUI);
        }
        
        /// <summary>
        /// 隐藏某个菜单
        /// </summary>
        /// <param name="_uIType"></param>
        public void HideUI(UIType _uIType)
        {
            if (uiDictionary.ContainsKey(_uIType))
            {
                uiDictionary[_uIType].SetActive(false);
            }
        }

        /// <summary>
        /// 显示某个菜单
        /// </summary>
        /// <param name="_uIType"></param>
        public void ShowUI(UIType _uIType)
        {
            if (uiDictionary.ContainsKey(_uIType))
            {
                uiDictionary[_uIType].SetActive(true);
            }
        }

        /// <summary>
        /// 注册菜单
        /// </summary>
        public void RegisterMenu(UIType _uIType,GameObject _uiObject)
        {
            if (!uiDictionary.ContainsKey(_uIType))
            {
                uiDictionary.Add(_uIType, _uiObject);
                //Debug.Log($"菜单注册成功: {_uIType}");
            }
        }

        /// <summary>
        /// 注销菜单
        /// </summary>
        public void UnregisterMenu(UIType _uIType)
        {
            if (uiDictionary.ContainsKey(_uIType))
            {
                uiDictionary.Remove(_uIType);
                //Debug.Log($"菜单注销成功: {_uIType}");
            }
        }

        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <param name="_uIType"></param>
        /// <returns></returns>
        public GameObject GetGameObject(UIType _uIType)
        {
            if (uiDictionary.ContainsKey(_uIType))
            {
                return uiDictionary[_uIType];
            }
            else
            {
                Debug.LogError($"没有找到菜单: {_uIType}");
                return null;
            }
        }
    }
}


